package com.kongfuzi.student.ui.gaspay;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kongfuzi.student.R;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.support.utils.UrlConstants;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;
import com.kongfuzi.student.bean.Order;
import com.kongfuzi.student.support.utils.RequestUtil;
import com.kongfuzi.student.support.volley.Response;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GasOrderActivity extends CustomerActionBarActivity {

    @Bind(R.id.lv)
    ListView mLv;

    public static Intent newIntent() {
        Intent intent = new Intent(GasApplication.getInstance(), GasOrderActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gas_order);
        ButterKnife.bind(this);
        getData();
    }

    private void getData() {
        showProgressDialog("正在加载...");
        String requestUrl = UrlConstants.ORDER_LIST + "&id=" + GasApplication.getUserId();
        RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                dismissProgressDialog();
                if (response != null && response.optBoolean("success")) {
                    Gson gson = new Gson();
                    List<Order> list = gson.fromJson(response.optJSONArray("data").toString(), new
                            TypeToken<List<Order>>() {
                            }.getType());
                    mLv.setAdapter(new GasOrderAdapter(list,GasOrderActivity.this));
                }
            }
        }, null, mTag);
    }

    private class GasOrderAdapter extends BaseAdapter {

        private List<Order> mList;
        private Context mContext;

        public  GasOrderAdapter(List<Order> list,Context context) {
            this.mList = list;
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Order getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holer = null;
            if(convertView == null) {
                convertView = View.inflate(mContext, R.layout.layout_item_gas_order, null);
                holer = new ViewHolder();
                holer.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                holer.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
                holer.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                holer.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
                convertView.setTag(holer);
            } else {
                holer = (ViewHolder) convertView.getTag();
            }
            Order order = mList.get(position);
            holer.tv_title.setText(order.ordtitle);
            holer.tv_money.setText(order.ordfee);
            holer.tv_time.setText(order.ordtime);
            holer.tv_type.setText(order.etype);
            return convertView;
        }
    }

    static class ViewHolder {
        public TextView tv_title,tv_money,tv_time,tv_type;
    }

}
